<?php

/*
Copyright 2009-2011 Sam Weiss
All Rights Reserved.

This file is part of Spark/Plug.

Spark/Plug is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('spark/plug'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

// -----------------------------------------------------------------------------

class _SparkLogger extends SparkPlug
{
	const kLogLevel_info = 0;
	const kLogLevel_notice = 1;
	const kLogLevel_warning = 2;
	const kLogLevel_error = 3;
	const kLogLevel_critical = 4;
	
	private $_enabled;
	private $_threshold;
	private $_path;
	private $_php_error_log;
	private $_email;
	private $_mailer;

	// --------------------------------------------------------------------------

	public function __construct($params = NULL, $mailer = NULL)
	{
		parent::__construct();

		$this->_enabled = SparkConfig::paramGet($params, 'enabled', SparkConfig::php_ini_get('log_errors') === true);
		$this->_threshold = SparkConfig::paramGet($params, 'threshold', 0);
		$this->_path = SparkConfig::paramGet($params, 'path', '');
		$this->_php_error_log = !empty($params['php_error_log']);
		$this->_email_subject = SparkConfig::paramGet($params, 'email_subject', '');
		$this->_email_name = SparkConfig::paramGet($params, 'email_name', 'SparkLogger');
		$this->_email_from = SparkConfig::paramGet($params, 'email_from', '');
		$this->_email_to = SparkConfig::paramGet($params, 'email_to', '');
		$this->_mailer = $mailer;
	}

	// --------------------------------------------------------------------------

	public function enabled()
	{
		return $this->_enabled;
	}
	
	// --------------------------------------------------------------------------

	public function enable()
	{
		$this->_enabled = true;
	}
	
	// --------------------------------------------------------------------------

	public function disable()
	{
		$this->_enabled = false;
	}
	
	// --------------------------------------------------------------------------

	public function write($msg, $level = 0, $sendEmail = false, $sendEmailTo = NULL)
	{
		if (!$this->_enabled || ($level < $this->_threshold))
		{
			return;
		}
		
		if ($this->_path)
		{
			error_log($msg, 3, $this->_path);
		}
		
		if ($this->_php_error_log)
		{
			error_log($msg, 0);
		}
		
		if ($sendEmail && ($mailTo = $sendEmailTo ? $sendEmailTo : $this->_email_to))
		{
			if ($this->_mailer)
			{
				$this->_mailer->isHTML(false)->sender($this->_email_from)->from($this->_email_from)->fromName($this->_email_name)->addAddress($mailTo);
				$this->_mailer->subject($this->_email_subject)->body($msg);
				$this->_mailer->send();
			}
			else
			{
				$headers = "Subject: {$this->_email_subject}\r\n" . "From: {$this->_email_from}\r\n" . "Reply-To: {$this->_email_from}\r\n";
				error_log($msg, 1, $mailTo, $headers);
			}
		}
	}

	// --------------------------------------------------------------------------
}
